package de.baumann.browser.activitys.gg

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import de.baumann.browser.R
import de.baumann.browser.activitys.user.BindETHActivity
import de.baumann.browser.adapter.MortgageTimeAdapter
import de.baumann.browser.adapter.SpSmelterAdapter
import de.baumann.browser.api.net.vo.gg.Smelter
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.gg.IJoinSmelterView
import de.baumann.browser.present.gg.JoinSmelterPresenter
import de.baumann.browser.utils.getUser
import de.baumann.browser.utils.web3j.TokenUtil
import de.baumann.browser.utils.web3j.WalletDaoUtils
import de.baumann.browser.views.dialog.BindEthDialog
import de.baumann.browser.views.dialog.PayPwdDialog
import kotlinx.android.synthetic.main.activity_join_smelter.*
import kotlinx.android.synthetic.main.include_tool_bar.*

class JoinSmelterActivity : BaseOdinActivity<IJoinSmelterView, JoinSmelterPresenter>(), IJoinSmelterView {

    companion object {
        fun startJoinSmelterActivity(context: Context,wallet:ETHWallet) {
            context.startActivity(Intent(context, JoinSmelterActivity::class.java).apply {
                putExtras(Bundle().apply {
                    putSerializable("wallet",wallet)
                })
            })
        }
    }

    private var qvlaueInt = 1
    private lateinit var mortgageTimeAdapter: MortgageTimeAdapter
    private lateinit var payPwdDialog: PayPwdDialog
    private lateinit var spSmelterAdapter: SpSmelterAdapter

    override fun getTitleText(): String {
        return "Join smelter"
    }

    override fun getLayout(): Int {
        return R.layout.activity_join_smelter
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        ivJian.setOnClickListener {
            if (qvlaueInt <= 1) {
                return@setOnClickListener
            }
            qvlaueInt -= 1
            qvlaue.text = qvlaueInt.toString()
        }

        ivJia.setOnClickListener {
            qvlaueInt += 1
            qvlaue.text = qvlaueInt.toString()
        }

        mortgageTimeAdapter = MortgageTimeAdapter()
        rvMortgageTime.adapter = mortgageTimeAdapter

        mortgageTimeAdapter.setNewData(arrayListOf("3", "6", "9", "12"))

        btnJoinSmelter.setOnClickListener {
            payPwdDialog = PayPwdDialog(mContext, object : PayPwdDialog.PwdCallback {
                override fun callback(pwd: String) {
                    try {
                        val privateKey = TokenUtil.getPrivateKey(pwd, getWallet())
                        presenter.joinSmelterTransferToken(getWallet().address, privateKey, getSmelterAddress())
                    } catch (e: Exception) {
                        if ("Invalid password provided" == e.message) {
                            Toast.makeText(mContext, "Wrong Password", Toast.LENGTH_SHORT).show()
                        } else {
                            Toast.makeText(mContext, e.message, Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            })

            payPwdDialog.show()
        }
    }

    override fun initData() {
        presenter?.smelteryRank()
        presenter?.getRule()
    }

    override fun createPresenter(): JoinSmelterPresenter {
        return JoinSmelterPresenter()
    }

    override fun createView(): IJoinSmelterView {
        return this
    }

    override fun setSmelters(list: List<Smelter>) {
        spSmelterAdapter = SpSmelterAdapter(mContext, list)
        spinnerSmelter.adapter = spSmelterAdapter
    }

    override fun getSmelterAddress(): String {
        return (spinnerSmelter.selectedItem as Smelter).userAddress
    }

    override fun getId(): String {
        val smelter = spinnerSmelter.selectedItem as Smelter
        return smelter.smelteryId
    }

    override fun getQValue(): String {
        return (qvlaueInt * 10000).toString()
    }

    override fun getTime(): Long {
        return mortgageTimeAdapter.getSelectData()
    }

    override fun joinSuccess() {
        payPwdDialog.dismiss()
        showShortToast("Join Success")
        finish()
    }

    override fun setRule(rule: String) {
        tvRule.text = rule
    }

    private fun getWallet():ETHWallet{
        return intent.getSerializableExtra("wallet") as ETHWallet
    }

}